package com.zzyl.nursing.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-29
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    @Transactional
    public NursingPlanVo selectNursingPlanById(Long id) {
//        return getById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        //（因为有两张表，所以这里采用的是查两次）
        //1.查询护理计划表(nursing_plan表)
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);

        //2.查询护理计划关联的 护理项目列表
        //为什么查询的是nursing_project_plan表？而不是nursing_project表？
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectNursingPlanByPlanId(id);
        nursingPlanVo.setProjectPlans(list);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {
//    return save(nursingPlan)? 1:0;
        //保存护理计划数据
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        nursingPlan.setCreateTime(new Date());
        nursingPlanMapper.insertNursingPlan(nursingPlan);

        //保存护理计划的护理项目信息
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        //保存护理项目信息及id
        int count = nursingProjectPlanMapper.batchInsert(projectPlans, nursingPlan.getId());
        return count > 0 ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
//        return updateById(nursingPlan) ? 1 : 0;
        //1.修改护理计划表
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        nursingPlanMapper.updateNursingPlan(nursingPlan);
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if(projectPlans != null && !projectPlans.isEmpty()){
            //2.修改护理计划项目表
            //由于涉及到的情况太多，因此先删除所有关联的护理项目，重新添加
            //2.1 删除所有关联的护理项目
            nursingProjectPlanMapper.deleteNursingProjectPlanByPlanId(nursingPlan.getId());
            //2.2 批量添加所有关联的护理项目
            if (projectPlans != null && !projectPlans.isEmpty()) {
                int count = nursingProjectPlanMapper.batchInsert(projectPlans, nursingPlan.getId());
                return count;
            }
        }
        return 1;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids) {
        return removeBatchByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        //分别删除两张表的内容
        int count = nursingPlanMapper.deleteNursingPlanById(id);
        nursingProjectPlanMapper.deleteNursingProjectPlanById(id);
        return count;
    }
}
